package kmeans;


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

class Point implements Cloneable{
    double x, y;
    Cluster c;

    Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public Cluster get_Cluster() {
        return this.c;
    }

    public void set_Cluster(Cluster c) {
        this.c = c;
    }
    public double distanceTo(Point other) {
        return Math.sqrt(Math.pow(this.x - other.x, 2) + Math.pow(this.y - other.y, 2));
    }

    public Point add(Point other) {
        return new Point(this.x + other.x, this.y + other.y);
    }

    public Point clone() {
        return new Point(this.x, this.y);
    }

}

public class Dataset {
    private int K = 3;

    public Dataset(int k) {
        this.K = k;
    }

    public List<Point> generate_centers(int n) {
        List<Point> centers = new ArrayList<>();
        Random random = new Random();

        for (int i = 0; i < n; ++i){
            centers.add(new Point(random.nextDouble() * 10, random.nextDouble() * 10));
        }
        return centers;
    }

    public List<Point> generate_samples(int n, Point center, double radius) {
        List<Point> points = new ArrayList<>();
        Random random = new Random();

        for (int i = 0; i < n; ++i){
            Point bias = new Point(random.nextDouble() * radius, random.nextDouble() * radius);
            points.add(center.add(bias));
        }
        return points;
    }

    public List<Point> load_data(String filePath){
        List<Point> points = new ArrayList<>();
        BufferedReader br=null;
        String line = null;

        try {
            br = new BufferedReader(new FileReader(filePath));

            //循环读取每行
            while ((line = br.readLine()) != null) {
                //按逗号分割
                String[] arr = line.split(",");
                Point point = new Point(Double.parseDouble(arr[0]), Double.parseDouble(arr[1]));
                points.add(point);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return points;
    }

    public static void main(String[] args) {
        Dataset dataset = new Dataset(3);
        System.out.printf("构造%d个类别的数据集\n", dataset.K);

        Random ran = new Random();
        List<Point> data = new ArrayList<>();
        List<Point> centers = dataset.generate_centers(dataset.K);
        for (Point center : centers) {
            System.out.printf("中心点：%f,%f\n", center.x, center.y);
            double radius =  1 + ran.nextInt(2);
            List<Point> points = dataset.generate_samples(20, center, radius);
            data.addAll(points);
        }

        try (FileWriter writer = new FileWriter("sample_data.csv")) {
            for (Point point : data) {
                String line = String.format("%f,%f\n", point.x, point.y);
                writer.write(line);
            }
            System.out.printf("已生成%d条样本数据并保存至 sample_data.csv\n", data.size());
        } catch (IOException e) {
            e.printStackTrace();
        }

        data = new ArrayList<>();
        data = dataset.load_data("sample_data.csv");
        System.out.printf("已加载%d条样本数据\n", data.size());
    }
}


